package org.docshare.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class BatteryTool extends BroadcastReceiver {

    private static BatteryTool ins=null;
	private IntentFilter batteryLevelFilter;
	int percent;  
    
	public int getPercent(){
		return percent;
	}
	public static BatteryTool  getIns(){
		if(ins == null){
			ins = new BatteryTool();
		}
		
		return ins;
	}
	public void start(Context ctx){
		batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);  
        ctx.registerReceiver(this, batteryLevelFilter); 
	}
	
	public void stop(Context ctx){
		ctx.unregisterReceiver(this);
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		//获取当前电量
		int level = intent.getIntExtra("level", 0);
		//电量的总刻度
		int scale = intent.getIntExtra("scale", 100);
		//把它转成百分比
		percent = (level*100)/scale;
	}
}
